package efg;

import interfacesFerrovia.ControleFerrovia;
import interfacesFerrovia.DefsGlobais.TipoAtributo;

import java.util.Map;

import efg.tasks.SchedulableTask;

public abstract class RailwayElement {

	private String id;
	private Railway railway;
	private Map<TipoAtributo, Object> staticAttributes;
	
	public RailwayElement(Railway r, String id) {
		assert(id != null);
		assert(r != null);
		this.id = id;
		
		this.railway = r;
		this.staticAttributes = getRailwayControl().consultaAtributosFixos(this.id);
		
		assert(this.id != null);
		assert(this.railway != null);
	}
	
	protected Object getStaticAttribute(TipoAtributo attribute) {
		assert(attribute != null && staticAttributes != null && staticAttributes.containsKey(attribute));
		Object value = staticAttributes.get(attribute);
		return value;
	}
	
	protected Object getAttribute(TipoAtributo attribute) {
		assert(attribute != null);
		assert(railway != null);
		return railway.getAttribute(id, attribute);
	}
	
	protected void registerTask(SchedulableTask task) {
		railway.registerTask(task);
	}
	
	public String getId() {
		assert(id != null);
		return id;
	}

	@Override
	public boolean equals(Object obj) {
		assert(id != null);
		if (obj == null) {
			return false;
		}
		if (! (obj instanceof RailwayElement)) {
			return false;
		} else {
			RailwayElement el = (RailwayElement) obj;
			assert(el.getId() != null);
			return el.getId().equals(id);
		}
	}

	@Override
	public int hashCode() {
		assert(id != null);
		return id.hashCode();
	}
	
	protected RailwayElement getElementById(String elId) {
		assert(railway != null);
		assert(elId != null);
		assert(! "".equals(elId));
		RailwayElement el = railway.getElementById(elId);
		assert(el != null);
		assert(el.getId().equals(elId));
		return el;
	}
	
	protected ControleFerrovia getRailwayControl() {
		return railway.getRailwayControl();
	}
	
	public String toString() {
		return getStaticAttribute(TipoAtributo.TIPO) + ":" + id;
	}
	
}
